<?php
/* 
 * class.mytalkBase.php
 * 我的碎碎念基础公共类
 * @version 0.1
 * @author samuel
 * @date 2009-05-25
 */
class mytalkBase {

    public $core ;
    public $params;
    public $error;

    function  __construct() {
        $this->core = $GLOBALS['CORE'];
        $this->params = $this->core->params;
        $this->error = "";
        $this->prompt = "";
    }

    //错误跳转
    function jumpError($error="", $pagetitle="", $backurl="" ){
    	$errors = $error ? $error : $this->error;
    	$page_title = $pagetitle ? $pagetitle : "JustFun-错误提示";
    	$backurl = $backurl ? htmlspecialchars($backurl) : htmlspecialchars($_SERVER['HTTP_REFERER']);
    	$content = $errors;
    	//header("Content-type: text/vnd.wap.wml");
    	$this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->assign('backurl', $backurl);
	    $this->core->tpl->display('info.wml');
    }
    
    //提示跳转
    function jumpPrompt($prompt="", $pagetitle="", $backurl=""){
        $prompts = $prompt ? $prompt : $this->prompt;
    	$page_title = $pagetitle ? $pagetitle : "JustFun-信息提示";
    	$backurl = $backurl ? htmlspecialchars($backurl) : htmlspecialchars($_SERVER['HTTP_REFERER']);
    	//header("Content-type: text/vnd.wap.wml");
    	$this->core->tpl->assign('content', $prompts);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->assign('backurl', $backurl);
	    $this->core->tpl->display('info.wml');
    }
    
    //获取关注的人
    function getUserAttention($user_id="", $attention="", $count=false, $arr_order="", $arr_limit="", $del=0){
        if(!$user_id && !$attention) return false;
        if($user_id) $append_user = " AND `UserID`='".$user_id."'";
        if($attention) $append_atten = " AND `AttentionUser`='".$attention."'";
        if($arr_order) $append_order = " ORDER BY `".$arr_order['attr']."` ".$arr_order['order'];
        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset']." , ".$arr_limit['pagesize'];
        if($count){
            $sql = "SELECT count(`TalkattentionID`) FROM `fun_talk_attention` WHERE 1 AND `del`='".$del."'" . $append_user . $append_atten;
            //echo $sql;
            $count = $this->core->db->get_var($sql);
            return $count;
        }else{
            $sql = "SELECT * FROM `fun_talk_attention` WHERE 1 AND `del`='".$del."'" . $append_user . $append_atten.$append_order.$append_limit;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            return $arr_infos;
        }
    }
    
    //获取碎碎念
    function getUserTalk($tid="", $user_id="", $IsIndexElite="", $count=false){
        if(!$tid && !$user_id) return false;
        if($tid) $append_tid = " AND `TalkID`='".$tid."'";
        if($user_id) $append_userid = " AND `UserID`='".$user_id."'";
        if($IsIndexElite) $append_iselite = " AND `IsIndexElite`='".$IsIndexElite."'";
        if($count){
            $sql = "SELECT count(`TalkID`) FROM `fun_talk` WHERE 1 ".$append_tid.$append_userid.$append_iselite;
            $count = $this->core->db->get_var($sql);
            return $count;
        }else{
            $sql = "SELECT * FROM `fun_talk` WHERE 1 ".$append_tid.$append_userid.$append_iselite;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            if($tid) return $arr_infos[0];
            return $arr_infos;
        }
    }
    
    //获取碎碎念回复
    function getUserFollowTalk($tfid="", $user_id="", $talk_id="", $isread="", $count=false){
        if(!$tfid && !user_id && !$talk_id) return false;
        if($tfid) $append_tfid = " AND `TalkFollowID`='".$tfid."'";
        if($user_id) $append_userid = " AND `UserID`='".$user_id."'";
        if($talk_id) $append_talkid = " AND `TalkID`='".$talk_id."'";
        if($isread) $append_isread = " AND `isRead`='".$isread."'";
        if($count){
            $sql = "SELECT count(`TalkFollowID`) FROM `fun_talk_follow` WHERE 1 ".$append_tfid.$append_userid.$append_talkid.$append_isread;
            $count = $this->core->db->get_var($sql);
            return $count;
        }else{
            $sql = "SELECT * FROM `fun_talk_follow` WHERE 1 ".$append_tfid.$append_userid.$append_talkid.$append_isread;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            if($tfid) return $arr_info[0];
            return $arr_infos;
        }
    }
    
    //获取某用户的碎碎念最新回复
    function getUserNewTalkFollow($user_id, $count=false){
        if(!$user_id) return false;
        $talks = $this->getUserTalk('', $user_id);
        $arr_tids = array();
        if($talks){
            foreach($talks as $talk){
                $arr_tids[] = $talk['TalkID'];
            }
            $str_tids = @implode(',', $arr_tids);
            if($count){
                $sql = "SELECT count(`TalkFollowID`) FROM `fun_talk_follow` WHERE `TalkID` IN (".$str_tids.") AND `isRead`='1' AND `Del` != '1' ORDER BY `FollowTime` DESC";
                $count = $this->core->db->get_var($sql);
                return $count;
            }else{
                $sql = "SELECT `TalkFollowID` FROM `fun_talk_follow` WHERE `TalkID` IN (".$str_tids.") AND `isRead`='1' AND `Del` != '1' ORDER BY `FollowTime` DESC";
                $res = $this->core->db->get_results($sql);
                $arr_infos = array();
        		if(!empty($res)){
                    foreach($res as $key => $row){
                        foreach($row as $k => $v){
                            $arr_infos[$key][$k] = $v;
                        }
                    }
                }
                return $arr_infos;
            }
        }else{
            return false;
        }
    }

    function getUserAllTalkInfo($arr_filter="", $arr_order="", $arr_limit="", $count=false){
        if($arr_filter){
            $append_where = "";
            foreach($arr_filter as $filter){
                $append_where .= " AND `".$filter['attr']."` ".$filter['oper']." '".$filter['value']."'";
            }
        }
        if($arr_order) $append_order = " ORDER BY `".$arr_order['attr']."` ".$arr_order['order'];
        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset']." , ".$arr_limit['pagesize'];
        if($count){
            $sql = "SELECT count(*) FROM `fun_view_usertalkfollow` WHERE 1 ".$append_where.$append_order.$append_limit;
            $count = $this->core->db->get_var($sql);
            return $count;
        }else{
            $sql = "SELECT * FROM `fun_view_usertalkfollow` WHERE 1 ".$append_where.$append_order.$append_limit;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
            if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            return $arr_infos;
        }
    }
    
//    function getUserAllTalkInfo($user_id, $arr_filter="", $arr_order="", $arr_limit="", $count=false){
//        if(!$user_id) return false;
//        if($arr_filter) $append_where = " AND `".$arr_filter['attr']."` ".$arr_filter['oper']." '".$arr_filter['value']."'";
//        if($arr_order) $append_order = " ORDER BY `".$arr_order['attr']."` ".$arr_order['order'];
//        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset']." , ".$arr_limit['pagesize'];
//        if($count){
//            $sql = 'SELECT count(*) FROM ( SELECT TalkID as aid , TalkID as tid , UserID as tuid, UserID as auid , TalkContent as acontent , TalkTime as atime, IsRead , 1 as isT FROM `fun_talk` WHERE `Del` = 0 AND `UserID`='.$user_id
//        . ' UNION '
//        . ' SELECT TalkFollowID as aid , TalkID as tid , TalkUserID as tuid, UserID as auid , FollowContent as acontent , FollowTime as atime , IsRead, 0 as isT FROM `fun_talk_follow` WHERE `Del` = 0 AND `TalkUserID`='.$user_id.' ) as B WHERE 1 '.$append_where . $append_order ;
//            //echo $sql;
//            $count = $this->core->db->get_var($sql);
//            return $count;
//        }else{
//            $sql = 'SELECT * FROM ( SELECT TalkID as aid , TalkID as tid , UserID as tuid, UserID as auid , TalkContent as acontent , TalkTime as atime , IsRead, 1 as isT FROM `fun_talk` WHERE `Del` = 0 AND `UserID`='.$user_id
//        . ' UNION '
//        . ' SELECT TalkFollowID as aid , TalkID as tid , TalkUserID as tuid, UserID as auid , FollowContent as acontent , FollowTime as atime , IsRead, 0 as isT FROM `fun_talk_follow` WHERE `Del` = 0 AND `TalkUserID`='.$user_id.' ) as B WHERE 1 '.$append_where . $append_order ;
//
//            $res = $this->core->db->get_results($sql);
//            $arr_infos = array();
//		    if(!empty($res)){
//                foreach($res as $key => $row){
//                    foreach($row as $k => $v){
//                        $arr_infos[$key][$k] = $v;
//                    }
//                }
//            }
//            return $arr_infos;
//        }
//    }
    
    //格式化时间，超过1天的才显示具体时间，1分钟内显示秒数，大于1小时显示时分
    function formatTalkTime($time){
        $time_stamp = strtotime($time);
        $now = time();
        $time_diff = $now - $time_stamp;
        $str = "";
        if($time_diff >= 24 * 60 * 60){
            $str = $time;        
        }elseif($time_diff >= 60 * 60){
            $hours = floor($time_diff/3600);
            $mins = floor(($time_diff-3600*$hours)/60);
            $secs = $time_diff - 3600*$hours - 60*$mins;
            $str .= $hours."小时".$mins."分钟".$secs."秒前";
        }elseif($time_diff >= 60){
            $mins = floor($time_diff/60);
            $secs = $time_diff - 60*$mins;
            $str .= $mins."分钟".$secs."秒前";
        }else{
            $str .= $time_diff."秒";
        }
        return $str;
    }
    
    //取消关注
    function delUserAttention($user_id, $fid){
        if(!$user_id || !$fid) return false;
        $sql = "UPDATE `fun_talk_attention` SET `Del`='1' WHERE `UserID`='".$user_id."' AND `AttentionUser`='".$fid."'";
        $res = $this->core->db->query($sql);
        return $res;
    }
    
    //添加关注
    function addUserAttention($user_id, $fid){
        if(!$user_id || !$fid) return false;
        //判断是否是删除过
        $tag = $this->getUserAttention($user_id, $fid, true, "", "", 1);
        if($tag){
            $sql = "UPDATE `fun_talk_attention` SET `Del`='0' WHERE `UserID`='".$user_id."' AND `AttentionUser`='".$fid."'";
        }else{
            $sql = "INSERT INTO `fun_talk_attention` SET `UserID`='".$user_id."', `AttentionUser`='".$fid."'";
        }
        $res = $this->core->db->query($sql);
        return $res;
    }
}
?>
